package indi.zhifa.recipe.bailan5.tools.controller.api;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.support.config.FastJsonConfig;
import com.alibaba.fastjson2.support.spring.http.converter.FastJsonHttpMessageConverter;
import indi.zhifa.recipe.bailan.framework.common.entity.annotations.ZfRestController;
import indi.zhifa.recipe.bailan.framework.common.entity.dto.RestResponse;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.math.BigInteger;

@Api(tags = "test-测试接口")
@RequestMapping("/api/math")
@Slf4j
@ZfRestController
@RequiredArgsConstructor
public class MathApi {

    BigInteger getFibonacii(int n){
        BigInteger n1 = new BigInteger("1");
        BigInteger n2 = new BigInteger("1");
        if(n==1){
            return n1;
        }else if(n==2){
            return n2;
        }else {
            BigInteger res = new BigInteger("1");
            for(int i=3;i<=n;i++){
                res = n1.add(n2);
                n1 = n2;
                n2 = res;
            }
            return res;
        }
    }

    @Operation(summary = "斐波那契数列")
    @GetMapping("/fibonacii/{n}")
    BigInteger fibonacii(@PathVariable(name = "n") int n){
        return getFibonacii(n);
    }

    @Operation(summary = "a+b")
    @GetMapping("/a_plus_b")
    Double aPlusB(@RequestParam(name = "a") double a,
                  @RequestParam(name = "b") double b){
        return a+b;
    }

    @Operation(summary = "超时测试")
    @GetMapping("/timeout/{t}")
    RestResponse<String> timeoutTest(@PathVariable(name = "t") int t) throws InterruptedException {
        Thread.sleep(t);
        return RestResponse.ok("睡了"+t/1000.0+"s，我睡醒了");
    }

}
